﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace WindowsFormsApp1
{
    public partial class Mainform : Form
    {
        public Mainform()
        {
            InitializeComponent();
        }

        private void Mainform_Load(object sender, EventArgs e)
        {
            var sqlStu = "select * from Articles";
            var dt = DbHelper.GetDataTable(sqlStu);
            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
        }

        //查找
        private void button1_Click(object sender, EventArgs e)
        {
            var title = textBox1.Text;

            var sql = string.Format("select * from Articles where Title like '%{0}%'", title);

            var dt = DbHelper.GetDataTable(sql);

            dataGridView1.DataSource = dt;
        }

        //添加
        private void button2_Click(object sender, EventArgs e)
        {
            Addform addform = new Addform();
            var res = addform.ShowDialog();

            if (res == DialogResult.Yes)
            {
                var title = textBox1.Text;                
                var sqlStu = string.Format("select * from Articles where Title like '%{0}%'", title);
                var dt = DbHelper.GetDataTable(sqlStu);
                dataGridView1.DataSource = dt;
            }
            else
            {
                this.Close();
            }
        }


        //更新
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var createdtime = (string)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;

                Addform addform = new Addform(id, title, author, createdtime);
                var res = addform.ShowDialog();

                if (res == DialogResult.Yes)
                {
                    var tit = textBox1.Text;
                    var sqlStu = string.Format("select * from Articles where Title like '%{0}%'", tit);
                    var dt = DbHelper.GetDataTable(sqlStu);
                    dataGridView1.DataSource = dt;
                }
                else
                {
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("你未选择你要更新的数据", "提示");
            }
        }

        //删除
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];

                var id = (int)row.Cells["Id"].Value;

                var res = MessageBox.Show("确定删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);


                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id={0}", id);
                    var rowCount = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (rowCount > 0)
                    {
                        var title = textBox1.Text;
                        var sqlStu = string.Format("select * from Articles where Title like '%{0}%'", title);
                        var dt = DbHelper.GetDataTable(sqlStu);
                        dataGridView1.DataSource = dt;
                        MessageBox.Show("删除成功", "提示");
                    }
                }
            }
            else
            {
                MessageBox.Show("你没有选择你要删除的行", "提示");
            }

        }
    }
}
